package com.fxsh168.server.activity;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;

import com.fxsh168.server.base.BaseActivity;
import com.fxsh168.server.interfaces.Constant;
import com.fxsh168.server.entitys.ModifyEntity;
import com.fxsh168.server.R;
import com.fxsh168.server.entitys.RegiYZMCG;
import com.fxsh168.server.utils.MD5Utils;
import com.fxsh168.server.utils.RegExp;
import com.google.gson.Gson;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.StringCallback;


import org.greenrobot.eventbus.EventBus;

import okhttp3.Call;

public class ChangePasswordActivity extends BaseActivity {

    private EditText mPutJiuMiMa, mPutXinMiMa, mPutChongFuXinMiMa;
    private String mGetJiuMiMa;
    private String mPhone, token, mGetXinMiMa, mGetChongXinMiMa;
    String mPassword, mGetJiaMiJiuMiMa, mGetJiaMiChongXinMiMa;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_modify_password);
        mPutJiuMiMa = (EditText) findViewById(R.id.putJiuMiMa);
        mPutXinMiMa = (EditText) findViewById(R.id.putXinMiMa);
        mPutChongFuXinMiMa = (EditText) findViewById(R.id.putChongFuXinMiMa);
    }

    public void btnQueRenXiuGai(View view) {
        SharedPreferences sp = getSharedPreferences(Constant.LOGINCLASS, MODE_PRIVATE);
        mPhone = sp.getString("mPhone", "");
        mPassword = sp.getString("mPassword", "");
        token = sp.getString("token", "");
        //获取输入框信息
        mGetJiuMiMa = mPutJiuMiMa.getText().toString().trim();
        mGetXinMiMa = mPutXinMiMa.getText().toString().trim();
        mGetChongXinMiMa = mPutChongFuXinMiMa.getText().toString().trim();
        //将新旧密码分别加密
        mGetJiaMiJiuMiMa = MD5Utils.MD532(mGetJiuMiMa);
        mGetJiaMiChongXinMiMa = MD5Utils.MD532(mGetChongXinMiMa);
        if ((mGetJiuMiMa != null && !TextUtils.isEmpty(mGetJiuMiMa)) && RegExp.checkPassword(mGetJiuMiMa)) {
            if ((mGetXinMiMa != null && !TextUtils.isEmpty(mGetXinMiMa)) && RegExp.checkPassword(mGetXinMiMa)) {
                if ((mGetChongXinMiMa != null && !TextUtils.isEmpty(mGetChongXinMiMa)) && RegExp.checkPassword(mGetChongXinMiMa) && mGetXinMiMa.equals(mGetChongXinMiMa)) {
                    OkHttpUtils.post().addParams("phone", mPhone).addParams("token", token)
                            .addParams("password", mGetJiaMiJiuMiMa).addParams("newpassword", mGetJiaMiChongXinMiMa)
                            .url(Constant.XIUGAIMIMA).build().execute(new StringCallback() {
                        @Override
                        public void onError(Call call, Exception e, int id) {
                            showToast("连接不上服务器!");
                        }

                        @Override
                        public void onResponse(String response, int id) {
                            if (response != null) {
                                RegiYZMCG regiYZMCG = new Gson().fromJson(response, RegiYZMCG.class);
                                String code = regiYZMCG.getCode().toString().trim();
                                if (code.equals(200 + "")) {
                                    showToast("修改成功!");
                                    String jMPassword2 = MD5Utils.MD532(mGetChongXinMiMa);
                                    //重新获取token
                                    String token2 = MD5Utils.MD532("lis" + mPhone + "bin" + jMPassword2 + "www625kejicom");
                                    ModifyEntity modifyEntity = new ModifyEntity(mPhone, mGetChongXinMiMa, token2);
                                    EventBus.getDefault().post(modifyEntity);
                                    Intent intent = new Intent(ChangePasswordActivity.this, LoginActivity.class);
                                    startActivity(intent);
                                    ChangePasswordActivity.this.finish();
                                } else if (code.equals(201 + "")) {
                                    showToast("非法请求!");
                                } else if (code.equals(202 + "")) {
                                    showToast("原密码错误!");
                                } else if (code.equals(203 + "")) {
                                    showToast("非法请求!");
                                } else if (code.equals(204 + "")) {
                                    showToast("密码修改失败!");
                                }
                            }
                        }
                    });

                } else {
                    mPutChongFuXinMiMa.setError("请输入正确的重复密码!");
                }

            } else {
                mPutXinMiMa.setError("请输入正确的新密码!");
            }
        } else {
            mPutJiuMiMa.setError("请输入正确的原始密码!");
        }
    }

}
